package com.longyiqun.apitest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.ITest;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.internal.BaseTestMethod;

import java.lang.reflect.Field;
import java.util.List;

@SpringBootTest(classes = ApiTestApplication.class)
public class ApiTestApplicationScene extends AbstractTestNGSpringContextTests implements ITest {

    @Autowired
    private testmapper testmapper;
    // 测试用例名称
    protected String mTestCaseName = "";
    private int i = 0;
    Object[] Name = new Object[500];


    @DataProvider(name = "RunScene")
    public Object[][] getCaseByScene() {


        String inP = System.getProperty("sceneID");

        List<TestApiEntity> allCases = testmapper.findCaseApi(inP);


        Object [][] objArray = new Object[allCases.size()][];


        for(int i=0;i< allCases.size();i++){
            objArray[i] = new Object[1];
            objArray[i][0] = allCases.get(i);
            Name[i] = allCases.get(i).getName();
            YLog.RedGreen("name",allCases.get(i).getName());
            YLog.RedGreen("**url: ", allCases.get(i).getUrl());
        }

        return objArray;
    }

    @Test(dataProvider = "RunScene")
    public void testRunAll (TestApiEntity tc, ITestContext crx) {

        String env = System.getProperty("env");
        GetApiResult.PostScene(tc, "3");
    }
    @AfterMethod(alwaysRun = true)
    public void setResultTestName(ITestResult result) {
        try {
            BaseTestMethod baseTestMethod = (BaseTestMethod) result.getMethod();
            Field f = baseTestMethod.getClass().getSuperclass().getDeclaredField("m_methodName");
            f.setAccessible(true);
            f.set(baseTestMethod, Name[++i].toString());
            System.out.println("result");
        } catch (Exception e) {
            Reporter.log("Exception : " + e.getMessage());
        }
    }
    @Override
    public String getTestName() {
        return this.mTestCaseName;
    }
}
